<?php 

class Ext_Register_Validate_Username extends Ext_Validate_Abstract
{
    protected $_username;
    
    const FORMAT_ERROR        = 'usernameFormatError';
    protected $_messageTemplates = array(
        self::FORMAT_ERROR        => "'%name%' format error.",
    );
    
    public function __construct($username)
    {
        $this->_username = $username;
        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }
    
    public function isValid()
    {
        $validator = new Zend_Validate_Regex("/^[a-zA-Z0-9-_]+$/si");
        $validator->setMessage($this->_messager->createMessage(self::FORMAT_ERROR, array('name'=>$this->_username)), Zend_Validate_Regex::NOT_MATCH);
        $validatorChain = new Zend_Validate();
        $validatorChain
            ->addValidator(new Zend_Validate_NotEmpty(), true)
            ->addValidator($validator, true)
            ->addValidator(new Zend_Validate_StringLength(2, 22), true)
            ;
        if(!$validatorChain->isValid($this->_username)) {
            $this->addTitleMessages('Username', $validatorChain->getMessages());
            return false;
        }
        
        return true;
    }
}
